package com.itheima.controller;

import com.itheima.Service.ProductFavoriteService;
import com.itheima.pojo.ProductFavorite;
import com.itheima.pojo.Result;
import com.itheima.pojo.FavoriteQueryParam;
import com.itheima.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/favorites")
public class ProductFavoriteController {

    @Autowired
    private ProductFavoriteService favoriteService;

    /**
     * 添加商品到收藏
     */
    @PostMapping("/add")
    public Result add(@RequestBody ProductFavorite pf,@RequestHeader("token") String token) {
        Integer userId = BaseContext.getCurrentId();
        pf.setUserId(userId);
        log.info("添加商品到收藏{}", pf);
        favoriteService.add(pf);
        return Result.success();
    }

    /**
     * 取消收藏商品
     */
    @DeleteMapping("/cancel")
    public Result cancel(@RequestBody ProductFavorite pf) {
        log.info("取消收藏商品{}", pf);
        favoriteService.cancel(pf.getUserId(), pf.getProductId());
        return Result.success();
    }

    /**
     * 批量取消收藏
     */
    @PostMapping("/batch-delete")
    public Result batchCancel(@RequestParam Integer userId, @RequestBody List<Integer> productIds) {
        log.info("批量取消收藏，用户ID：{}，商品ID列表：{}", userId, productIds);
        favoriteService.batchCancel(userId, productIds);
        return Result.success();
    }

    /**
     * 获取用户收藏列表
     */
    @GetMapping("/list")
    public Result list(FavoriteQueryParam param) {
        log.info("获取用户收藏列表：{}", param);
        return Result.success(favoriteService.list(param));
    }

    /**
     * 检查商品是否已收藏
     */
    @GetMapping("/check/{productId}")
    public Result checkStatus(@PathVariable Integer productId, @RequestParam Integer userId) {
        log.info("检查商品收藏状态，用户ID：{}，商品ID：{}", userId, productId);
        Map<String, Boolean> result = new HashMap<>();
        result.put(productId.toString(), favoriteService.checkStatus(userId, productId));
        return Result.success(result);
    }

    /**
     * 批量检查商品收藏状态
     */
    @PostMapping("/batch-check")
    public Result batchCheckStatus(@RequestParam Integer userId, @RequestBody List<Integer> productIds) {
        log.info("批量检查商品收藏状态，用户ID：{}，商品数量：{}", userId, productIds.size());
        return Result.success(favoriteService.batchCheckStatus(userId, productIds));
    }
}